package com.design.观察者模式;

import java.util.ArrayList;
import java.util.List;

public class WechatServer implements Observerable {

	private List<Observer> list;
	private String message;

	WechatServer() {
		list = new ArrayList<>();
	}

	@Override
	public void registerObserver(Observer o) {
		list.add(o);
	}

	@Override
	public void removeObserver(Observer o) {
		if (!list.isEmpty()) {
			list.remove(o);
		}
	}

	@Override
	public void notifyObserver(EnumObserverType type) {
		list.forEach(e -> {
			switch (type) {
			case ALL:
				e.update(message);
				break;
			case DELETE:
				e.delete(message);
				break;
			case UPDATE:

				break;

			case INSERT:

				break;
			default:
				e.update(message);
				break;
			}

		});
	}

	/**
	 * 设置通知的消息
	 * 
	 * @param s
	 */
	public void setInfomation(String s, EnumObserverType type) {
		this.message = s;
		System.out.println("消息为：" + s);
		notifyObserver(type);
	}

}
